package com.lanyou.esb.cook.proxy.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.RowMapper;

import com.lanyou.esb.cook.proxy.entity.FieldSystem;
import com.lanyou.esb.cook.proxy.meta.EnableType;

/**
 * 领域系统持久层
 * 
 * @author Davey.wu
 */
public class FieldSystemDao extends BaseDao<FieldSystem> {

	private RowMapper<FieldSystem> mapper = new RowMapper<FieldSystem>() {
		public FieldSystem mapRow(ResultSet rs, int rowNum) throws SQLException {

			FieldSystem rcd = new FieldSystem();
			rcd.setId(rs.getString("FIELD_SYSTEM_ID"));
			rcd.setFieldId(rs.getString("FIELD_ID"));
			rcd.setCode(rs.getString("SYSTEM_CODE"));
			rcd.setName(rs.getString("SYSTEM_NAME"));
			rcd.setRemark(rs.getString("REMARK"));
			rcd.setDirectoryId(rs.getInt("R_DIRECTORY_ID"));
			rcd.setEnable(EnableType.ENABLE.ValueCode.equals(rs.getString("IS_ENABLE")));
			rcd.setCreator(rs.getString("CREATOR"));
			rcd.setCreatedDate(rs.getDate("CREATED_DATE"));
			rcd.setModifier(rs.getString("MODIFIER"));
			rcd.setLastUpdatedDate(rs.getDate("LAST_UPDATED_DATE"));
			rcd.setVersion(rs.getInt("VERSION"));

			return rcd;
		}
	};

	@Override
	public List<FieldSystem> findAll() {
		String sql = "select * from T_COOK_BU_FIELD_SYSTEM";
		List<FieldSystem> records = this.getJdbcTemplate().query(sql, mapper);
		return records;
	}

	@Override
	public FieldSystem findById(String id) {
		String sql = "select * from T_COOK_BU_FIELD_SYSTEM where FIELD_SYSTEM_ID=?";
		FieldSystem system = this.getJdbcTemplate().queryForObject(sql, mapper,
				id);
		return system;
	}

	@Override
	public FieldSystem findByCode(String code) {
		if (StringUtils.isBlank(code)) {
			return null;
		}
		String sql = "select * from T_COOK_BU_FIELD_SYSTEM where SYSTEM_CODE=?";
		FieldSystem system = this.getJdbcTemplate().queryForObject(sql, mapper,
				code);
		return system;
	}

	@Override
	public FieldSystem find(FieldSystem system) {
		if (system == null) {
			return null;
		}
		return findByCode(system.getCode());
	}

}
